#!/usr/bin/python
# -*- coding: utf-8 -*-

__all__ = [u'CliPart']

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from optparse import make_option
from handler import HandlerPart

from pylot.core import (ENCODING, uprint, hardbook)
from pylot.cli import CliInterface, pyLotOptionGroup

class CliPart(CliInterface):

  def __init__ (self, handler=None, parser=None):
    if handler is None :
      handler = HandlerPart()
      handler.init()
    CliInterface.__init__(self, handler=handler, parser=parser)

    self.version = '0.1'
    self.description = self.tr_(u'DESC')
    self.usage = 'prog [options]'

    self._createOptions()

  def _createOptions(self):
    self.action_build_pylot = make_option("-B", "--build-pylot",
      dest="build_pylot", action="store_true")
    self.action_install_pylot = make_option("-I", "--install-pylot",
      dest="install_pylot", action="store", metavar='PYLOTPATH',)
    self.action_cleancache = make_option("--clean-cache",
      action="store_true", dest="cleancache")
    self.action_shared = make_option("--shared",
      dest="shared", action="store_true")
    self.action_static = make_option("--static",
      dest="static", action="store_true")
    self.action_debug_mode = make_option("--debug-mode",
      dest="debug_mode", action="store_true", default=False)

  def _createOptionGroups(self):
    group = pyLotOptionGroup(self.parser, u'group_build')
    self.group_build = self.addGroup(group)

  def _fillGroups(self):
    self.group_build.add_option(self.action_build_pylot, userlist=self.parser.console)
    self.group_build.add_option(self.action_install_pylot, userlist=self.parser.console)
    self.group_build.add_option(self.action_cleancache, userlist=self.parser.console)
    self.group_build.add_option(self.action_shared, userlist=self.parser.console)
    self.group_build.add_option(self.action_static, userlist=self.parser.console)
    self.group_build.add_option(self.action_debug_mode, userlist=self.parser.console)

  def _runOptions(self, options):
    if options.build_pylot or options.install_pylot :
      self.handler.updateCMakeOptions()
      self.handler.loadCMakeOptions()

      if options.shared is True :
        self.handler.cmake_options[u'BUILD_SHARED_LIBS'] = True
      elif options.static is True :
        self.handler.cmake_options[u'BUILD_SHARED_LIBS'] = False
      else : shared = None

      if options.install_pylot :
        install_dir = Path(options.install_pylot.decode(ENCODING)).abspath()
        self.handler.cmake_options[u'CMAKE_INSTALL_PREFIX'] = install_dir

      if options.build_pylot :
        self.handler.build(target=u'', debug=options.debug_mode)
      elif options.install_pylot :
        self.handler.build(target=u'install', debug=options.debug_mode)

  def _translate(self):
    self._setTexts([
      (self.group_build, self.tr_(u'CMake, Make, etc.')),

      (self.action_build_pylot, self.tr_(u'Compile and install all pylot libs')),
      (self.action_install_pylot, self.tr_(u'Make install all pylot libs')),
      (self.action_cleancache, self.tr_(u'Remove CMakeCache')),
      (self.action_shared, self.tr_(u'Compile shared libraries')),
      (self.action_static, self.tr_(u'Compile static libraries')),
      (self.action_debug_mode, self.tr_(u'Compile libraries in debug mode')),
      ])

    self._setTexts([
      (self.group_build, self.tr_(u'Compilation options'))
      ], field=u'title')

if __name__ == '__main__':
  u"""
  Launching CLI
  """
  from _pylot.cli.tools.utils import launch_cli_module
  parser, cli, options = launch_cli_module(CliPart)
  cli.runOptions(options)
